//
//  ViewController.m
//  UseLibSqlit3
//
//  Created by honeyrr on 2017/9/22.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "ViewController.h"
#import <sqlite3.h>

#import "NSString+WordsClass.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *wordsTableView;

@property (nonatomic, strong) NSArray *wordsClassArr;

@end

@implementation ViewController

-(NSArray *)wordsClassArr
{
    if (!_wordsClassArr) {
        _wordsClassArr = [NSArray array];
    }
    return _wordsClassArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self wordsClassArr];
    [self readSqlitFile];
    [self.wordsTableView reloadData];
}

-(void) readSqlitFile
{
    //获取db path
    NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"words.db" ofType:nil];
    //创建db
    sqlite3 *db;
    //判断是否能够打开db
    if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
        NSLog(@"sqlite dadabase is opened.");
    } else {
        NSLog(@"sqlite dadabase open fail.");
    }
    
    sqlite3_stmt *statement;
    NSMutableArray *wordsArr = [[NSMutableArray alloc]init];
    // @"select * from words"  查询所有 key 值内容
    NSString *selectSql = @"select id, en TEXT, zh TEXT from words";
    if (sqlite3_prepare(db, [selectSql UTF8String], -1, &statement, nil) == SQLITE_OK) {
        //遍历db-table每一行
        while (sqlite3_step(statement) == SQLITE_ROW) {
            // 查询 id 的值
            int _id = sqlite3_column_int(statement, 0);
            // 查询 en 的值
            NSString *en = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            // 查询 zh 的值
            NSString *zh = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
            NSDictionary *dict = @{@"_id":[NSString stringWithFormat:@"%d",_id], @"en":en, @"zh":zh};
            [wordsArr addObject:dict];
        }
        NSLog(@"wordsArr:%@",wordsArr);
        self.wordsClassArr = [NSString wordsClass:wordsArr];
        //NSLog(@"%@",self.wordsClassArr);
    } else {
        NSLog(@"error");
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.wordsClassArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [[self.wordsClassArr[section] allKeys] firstObject];
    return [[self.wordsClassArr[section] objectForKey:key] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    NSString *key = [[self.wordsClassArr[indexPath.section] allKeys] firstObject];
    NSArray *values = [self.wordsClassArr[indexPath.section] objectForKey:key];
    cell.textLabel.text = values[indexPath.row][@"en"];
    cell.detailTextLabel.text = values[indexPath.row][@"zh"];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[self.wordsClassArr[section] allKeys] firstObject];
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *indexArr = [NSMutableArray new];
    for (NSDictionary *dict in _wordsClassArr) {
        [indexArr addObject:[dict allKeys][0]];
    }
    return indexArr;
}
@end
